package com.andy.nicefi.validators;

import java.util.regex.Pattern;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class UserNameValidator extends FieldValidatorSupport{
	
	public static final String userNamePattern = "[_a-zA-Z][_a-zA-Z0-9]*";

	public void validate(Object object) throws ValidationException {
		String fieldName = getFieldName();
		Object value = getFieldValue(fieldName, object);
		
		if (value == null || value.toString().length() <= 0) {
			return;
		}
		if(!Pattern.matches(userNamePattern, value.toString())) {
			addFieldError(fieldName, object);
		}
		
	}

}
